package comms;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import ap.*;

public class CommsTesting extends MIDlet {
	private static CommsTesting instance;
	private TextBox textBox;
	private ConnectionManager manager;
	private DatagramAgentConnection outgoing;
	private AgentConnection test;
	
	public CommsTesting() {
		super();
		CommsTesting.instance = this;
		textBox = new TextBox("Comms Test MIDlet", "Testing...", 25, 0);
		
		AgentPlatform plat = new AgentPlatform();
		AgentPkt test = new AgentPkt();
		test.setAgentString("AgentStringTest\n/n\n");
		manager = new ConnectionManager(plat);
		try {
			manager.listenForAgent("socket://:5754");
			//manager.listenForAgent("datagram://:7777");
			manager.forwardAgent(test, "datagram://192.168.0.2:57353", false);
			manager.forwardAgent(test, "datagram://192.168.0.2:57353", false);
			//manager.listenForAgent("socket://:7777");
			manager.forwardAgent(test, "192.168.0.2");
			manager.forwardAgent(new MobileAgent("TEST"), "192.168.0.2");
		/*	manager.forwardAgent(test, "socket://192.168.0.2:5754", false);
			manager.forwardAgent(test, "socket://192.168.0.2:5754", false);
			manager.forwardAgent(test, "socket://192.168.0.2:57354", false);*/
			
		} catch (URIException e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
		}
		
		try {
			synchronized (this) {
				this.wait(1000);
				//manager.stop();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public static CommsTesting getInstance() {
		return CommsTesting.instance;
	}
	
	public void startApp() {
		this.display();
		
	}
	
	public void pauseApp() {
		
	}
	
	public void display() {
		Display.getDisplay(this).setCurrent(textBox);
	}
	
	public void destroyApp(boolean dest) {
		CommsTesting.instance = null;
	}
} 
